// 分类模型
const { CategoryOne,CategoryTwo,CategoryThree } = require('../../../models/category');
// 统一数据返回类
const { DataReturn } = require('../../../common/returnclass');

/**
 * @api {Get} /categories 查询分类
 * @apiGroup Admin-Categories
 *
 * @apiParam {Number} pagenum 数量
 * @apiParam {Number} pagesize 页数
 *
 * @apiSuccess {ObjectId} _id 分类id
 * @apiSuccess {Number} cate_level 分类等级
 * @apiSuccess {String} title  分类名称
 * @apiSuccess {String} isValid  是否有效
 * @apiSuccess {String} createTime  创建时间
 * @apiSuccess {Array} children  子分类
 */
module.exports = async (req, res) => {
	
	if (!req.query.pagenum) {
		
		// populate: {
		// 	path: 'children',
		// 	model: CategoryThree
		// }
		// 查询所有分类信息
		let category = await CategoryOne.find().populate({ 
			path: 'children', model: CategoryTwo ,
			populate: {
				path: 'children',
				model: CategoryThree
			} 
		});
		// 响应 商品分类
		return DataReturn.getData(res,200, category,'获取分类列表成功')
	}
	
	
	let { pagenum, pagesize } = req.query;
	pagenum = parseInt(pagenum);
	pagesize = parseInt(pagesize);
	console.log(req.query, pagesize*(pagenum-1));
	
	let category;
	// 根据用户名模糊查询
	category = await CategoryOne.find().populate({ 
		path: 'children', model: CategoryTwo ,
		populate: {
			path: 'children',
			model: CategoryThree
		} 
	}).skip(pagesize*(pagenum-1)).limit(pagesize);
	
	if (!category) return DataReturn.getData(res, 200, [], '暂无数据');
	// 响应
	DataReturn.getData(res, 200, { data:category, total: await CategoryOne.find().countDocuments() }, '获取分类列表成功')
};